<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>iter.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_iter.js.html">iter.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2008 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Iterators over DOM nodes.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.dom.iter.AncestorIterator&#39;);
<a name="line21"></a>goog.provide(&#39;goog.dom.iter.ChildIterator&#39;);
<a name="line22"></a>goog.provide(&#39;goog.dom.iter.SiblingIterator&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.iter.Iterator&#39;);
<a name="line25"></a>goog.require(&#39;goog.iter.StopIteration&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Iterator over a Node&#39;s siblings.
<a name="line30"></a> * @param {Node?} node The node to start with.
<a name="line31"></a> * @param {boolean} opt_includeNode Whether to return the given node as the
<a name="line32"></a> *     first return value from next.
<a name="line33"></a> * @param {boolean} opt_reverse Whether to traverse siblings in reverse
<a name="line34"></a> *     document order.
<a name="line35"></a> * @constructor
<a name="line36"></a> * @extends {goog.iter.Iterator}
<a name="line37"></a> */
<a name="line38"></a>goog.dom.iter.SiblingIterator = function(node, opt_includeNode, opt_reverse) {
<a name="line39"></a>  /**
<a name="line40"></a>   * The current node, or null if iteration is finished.
<a name="line41"></a>   * @type {Node?}
<a name="line42"></a>   * @private
<a name="line43"></a>   */
<a name="line44"></a>  this.node_ = node;
<a name="line45"></a>
<a name="line46"></a>  /**
<a name="line47"></a>   * Whether to iterate in reverse.
<a name="line48"></a>   * @type {boolean}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.reverse_ = !!opt_reverse;
<a name="line52"></a>
<a name="line53"></a>  if (node &amp;&amp; !opt_includeNode) {
<a name="line54"></a>    this.next();
<a name="line55"></a>  }
<a name="line56"></a>};
<a name="line57"></a>goog.inherits(goog.dom.iter.SiblingIterator, goog.iter.Iterator);
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/** @inheritDoc */
<a name="line61"></a>goog.dom.iter.SiblingIterator.prototype.next = function() {
<a name="line62"></a>  var node = this.node_;
<a name="line63"></a>  if (!node) {
<a name="line64"></a>    throw goog.iter.StopIteration;
<a name="line65"></a>  }
<a name="line66"></a>  this.node_ = this.reverse_ ? node.previousSibling : node.nextSibling;
<a name="line67"></a>  return node;
<a name="line68"></a>};
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Iterator over an Element&#39;s children.
<a name="line73"></a> * @param {Element} element The element to iterate over.
<a name="line74"></a> * @param {boolean} opt_reverse Optionally traverse children from last to
<a name="line75"></a> *     first.
<a name="line76"></a> * @param {number} opt_startIndex Optional starting index.
<a name="line77"></a> * @constructor
<a name="line78"></a> * @extends {goog.dom.iter.SiblingIterator}
<a name="line79"></a> */
<a name="line80"></a>goog.dom.iter.ChildIterator = function(element, opt_reverse, opt_startIndex) {
<a name="line81"></a>  if (!goog.isDef(opt_startIndex)) {
<a name="line82"></a>    opt_startIndex = opt_reverse &amp;&amp; element.childNodes.length ?
<a name="line83"></a>        element.childNodes.length - 1 : 0;
<a name="line84"></a>  }
<a name="line85"></a>  goog.dom.iter.SiblingIterator.call(this, element.childNodes[opt_startIndex],
<a name="line86"></a>      true, opt_reverse);
<a name="line87"></a>};
<a name="line88"></a>goog.inherits(goog.dom.iter.ChildIterator, goog.dom.iter.SiblingIterator);
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * Iterator over a Node&#39;s ancestors, stopping after the document body.
<a name="line93"></a> * @param {Node?} node The node to start with.
<a name="line94"></a> * @param {boolean} opt_includeNode Whether to return the given node as the
<a name="line95"></a> *     first return value from next.
<a name="line96"></a> * @constructor
<a name="line97"></a> * @extends {goog.iter.Iterator}
<a name="line98"></a> */
<a name="line99"></a>goog.dom.iter.AncestorIterator = function(node, opt_includeNode) {
<a name="line100"></a>  /**
<a name="line101"></a>   * The current node, or null if iteration is finished.
<a name="line102"></a>   * @type {Node?}
<a name="line103"></a>   * @private
<a name="line104"></a>   */
<a name="line105"></a>  this.node_ = node;
<a name="line106"></a>
<a name="line107"></a>  if (node &amp;&amp; !opt_includeNode) {
<a name="line108"></a>    this.next();
<a name="line109"></a>  }
<a name="line110"></a>};
<a name="line111"></a>goog.inherits(goog.dom.iter.AncestorIterator, goog.iter.Iterator);
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/** @inheritDoc */
<a name="line115"></a>goog.dom.iter.AncestorIterator.prototype.next = function() {
<a name="line116"></a>  var node = this.node_;
<a name="line117"></a>  if (!node) {
<a name="line118"></a>    throw goog.iter.StopIteration;
<a name="line119"></a>  }
<a name="line120"></a>  this.node_ = node.parentNode;
<a name="line121"></a>  return node;
<a name="line122"></a>};
</pre>


</body>
</html>
